package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MemberMapper;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.dao.SetmealMapper;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.*;
import com.itheima.health.service.OrderService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 预约
     * @param orderSubmitParam
     */
    public Order submit(OrderSubmitParam orderSubmitParam) {
        //判断是否为新用户
        Member member = memberMapper.selectByIdCard(orderSubmitParam.getIdCard());

        //会员id
        Integer id = null;
        //如果是新用户就往会员表中插入数据
        if(member == null){
            LocalDate date = LocalDate.now();
            Date regTime = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
            String birthdayString = orderSubmitParam.getIdCard().substring(6,14);
            DateTimeFormatter localDate = DateTimeFormatter.ofPattern("yyyyMMdd");
            LocalDate birthdayL = LocalDate.parse(birthdayString,localDate);
            Date birthday = Date.from(birthdayL.atStartOfDay(ZoneId.systemDefault()).toInstant());

            Member member1 = new Member();
            member1.setName(orderSubmitParam.getName());
            member1.setSex(orderSubmitParam.getSex());
            member1.setPhoneNumber(orderSubmitParam.getTelephone());
            member1.setIdCard(orderSubmitParam.getIdCard());
            member1.setRegTime(regTime);
            member1.setBirthday(birthday);

            memberMapper.insert(member1);
            Member member2 = memberMapper.selectByIdCard(orderSubmitParam.getIdCard());
            id = member2.getId();
        }else {
            id = member.getId();
        }
        LocalDate date = LocalDate.now();
        Date date1 = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
        //查询是否存在订单
        List<Order> orders = orderMapper.selectByMemberId(id);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if(orders != null && orders.size() > 0){
            for (Order order : orders) {
                //查询套餐是否相同
                if(orderSubmitParam.getSetMealId().equals(order.getSetmealId())){
                    //判断是否到诊
                    if(order.getOrderStatus().equals(Order.ORDERSTATUS_NO)){
                        //判断日期是否相同
                        String date2 = format.format(date1);
                        String date3 = format.format(order.getOrderDate());
                        if(date2.equals(date3)){
                            throw new BusinessRuntimeException(MessageConst.HAS_ORDERED);
                        }
                    }
                }

            }
        }
        //获取当日预约信息
        OrderSetting orderSetting = orderSettingMapper.find(orderSubmitParam.getOrderDate());
        if(orderSetting == null){
            //当日未设置预约信息
            throw new BusinessRuntimeException(MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }
        //预约已满
        if(orderSetting.getReservations() >= orderSetting.getNumber()){
            throw new BusinessRuntimeException(MessageConst.ORDER_FULL);
        }
        //更新预约人数信息
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingMapper.update(orderSetting);
        //插入订单表
        Order order = new Order();
        order.setMemberId(id);

        order.setOrderDate(date1);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(orderSubmitParam.getSetMealId());
        orderMapper.insert(order);
        Order order1 = orderMapper.findByMemberId(id);
        return order1;
    }

    /**
     * 通过id查找
     * @param id
     * @return
     */
    public OrderVO findById(Integer id) {
        Order order = orderMapper.findByMemberId(id);
        Member member = memberMapper.findById(order.getMemberId());
        Setmeal setmeal = setmealMapper.findById(order.getSetmealId());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = order.getOrderDate();
        String date = formatter.format(date1);
        OrderVO orderVO = new OrderVO();
        orderVO.setMember(member.getName());
        orderVO.setSetmeal(setmeal.getName());
        orderVO.setOrderDate(date);
        orderVO.setOrderType(order.getOrderType());
        return orderVO;
    }
}
